package com.apop.xiamen.controller;

import com.alibaba.fastjson.JSON;
import com.apop.common.exception.BizException;
import com.apop.common.model.Result;
import com.apop.demo.encrypt.EncryptTypeEnum;
import com.apop.demo.encrypt.EncryptionStrategy;
import com.apop.demo.encrypt.EncryptionStrategyFactory;
import com.apop.demo.encrypt.pojo.DecodeDataDto;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description:
 * @Author: zjy
 * @Date: 2025/3/17 9:17
 */
@RestController
@Slf4j
@Api(tags = "测试控制器")
@RequestMapping("/bank")
public class BankMockController {

    @Resource
    private EncryptionStrategyFactory encryptionStrategyFactory;

    /**
     * 保函申请订单
     * @param map
     * @return
     */
    @PostMapping("/guarantee_order_push")
    @ResponseBody
    public Result orderPush(@RequestBody Map map) {
        String plainMsg = decodeMsg(map);

        log.info("开启后台线程处理业务逻辑，过5秒将处理结果返回 ");
        // 开启后台线程，过5秒将处理结果返回

        return Result.success(null);
    }

    @PostMapping("/verify_guarantee")
    @ResponseBody
    public Result verifyGuarantee(@RequestBody Map map) {
        String plainMsg = decodeMsg(map);

        log.info("保函验真 ");
        // 开启后台线程，过5秒将处理结果返回

        return Result.success(null);
    }
    // 加密策略
    static EncryptTypeEnum encryptStrategy = EncryptTypeEnum.NONE;

    private String decodeMsg(Map map){
        String param = JSON.toJSONString(map);
        log.info("对方请求参数 {} ", param);

        /**
         * 由于加解密会耗费一定cpu性能，因此开发阶段通常设置为不加密传输
         */
        if (!encryptStrategy.equals(EncryptTypeEnum.NONE)){

            EncryptionStrategy encryptionStrategy = encryptionStrategyFactory.getEncryptionStrategy(encryptStrategy);
            {
                String sm2PriKey = "Uqe/JkbQYvdyzWEXQTHtuJ3qw3s9zJZrfWf5kTw38nw=";
                String sm3PublicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEKu01CrAZis5O6ofOSjxnOXnmza5YUjlkHbUuaeg6Xy8gngE1aSGionJkopPZ252LkCdE90XaA78QBKCNYRemaw==";

                // 解密
                DecodeDataDto decodeDataDto = new DecodeDataDto();
                decodeDataDto.setEncodeData(param);
                decodeDataDto.setSelfSm2PrivateKey(sm2PriKey);
                decodeDataDto.setOppositeSm3PublicKey(sm3PublicKey);
                String plainMsg = encryptionStrategy.decrypt(decodeDataDto);
                log.info("解密后 {} ", plainMsg);
                return plainMsg;
            }
        }
        return param;

    }

}
